package com.woniu.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.project.client.ProductEntityCient;
import com.woniu.project.core.domain.*;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dao.ProductBigTypeMapper;
import com.woniu.project.dto.productbigtype.ProductBigTypeSelectDto;
import com.woniu.project.params.productbigtype.ProductBigTypeAddParam;
import com.woniu.project.params.productbigtype.ProductBigTypeSelectParam;
import com.woniu.project.params.productbigtype.ProductBigTypeUpdateParam;
import com.woniu.project.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@Service
@Transactional
public class ProductBigTypeServiceImpl extends ServiceImpl<ProductBigTypeMapper, ProductBigType> implements IProductBigTypeService {

    @Autowired
    IIdentificationMapService identificationMapService;

    @Autowired
    IImageService imageService;

    @Autowired
    private ProductEntityCient productEntityCient;

    @Autowired
    private ProductSmallTypeServiceImpl productSmallTypeService;

    @Autowired
    private IProductBrandBigTypeService productBrandBigTypeService;

    @Autowired
    private IProductService productService;

    //根据是否有id和name进行不同的分页查询
    @Override
    public IPage<ProductBigTypeSelectDto> select(ProductBigTypeSelectParam productBigTypeSelectParam) {
        Page<ProductBigType> page = new Page<>(productBigTypeSelectParam.getPage(), productBigTypeSelectParam.getSize());
        QueryWrapper<ProductBigType> wrapper = new QueryWrapper<ProductBigType>();
        wrapper.eq(productBigTypeSelectParam.getId() != null && !productBigTypeSelectParam.getId().equals(""),
                "product_big_type_id", productBigTypeSelectParam.getId());

        wrapper.like(productBigTypeSelectParam.getName() != null && !productBigTypeSelectParam.getName().equals(""),
                "product_big_type_name", productBigTypeSelectParam.getName());

        wrapper.orderByAsc("product_big_type_sort");
        IPage ipage = page(page, wrapper);
        //entity换为dto
        List<ProductBigType> records = ipage.getRecords();
        List<ProductBigTypeSelectDto> productBigTypeSelectDtos = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ProductBigTypeSelectDto productBigTypeSelectDto = new ProductBigTypeSelectDto();
            BeanUtils.copyProperties(records.get(i), productBigTypeSelectDto);
            int imgId = records.get(i).getImageId();
            Image image = imageService.getById(imgId);
            productBigTypeSelectDto.setAddr(image.getImageAddr());
            productBigTypeSelectDtos.add(productBigTypeSelectDto);
        }
        ipage.setRecords(productBigTypeSelectDtos);
        return ipage;
    }

    //新增商品大类
    @Override
    public int addBigType(ProductBigTypeAddParam productBigTypeAddParam) {
        ProductBigType productBigType = new ProductBigType();
        BeanUtils.copyProperties(productBigTypeAddParam, productBigType);
        //图片增加
        Image image = new Image();
        String addr = productBigTypeAddParam.getAddr();
        image.setImageAddr(addr);
        imageService.save(image);

        //查询所有大类序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductBigType> wrapper = new QueryWrapper<ProductBigType>();
        wrapper.eq("product_big_type_sort", productBigType.getProductBigTypeSort());
        List<ProductBigType> list = list(wrapper);
        if (list.size() != 0) {
            return 1;
        }
        //大类增加
        productBigType.setImageId(image.getImageId());
        save(productBigType);
        //鉴定图定义增加
        List<IdentificationMap> identificationMaps = productBigTypeAddParam.getIdentificationMaps();
        for (int i = 0; i < identificationMaps.size(); i++) {
            identificationMaps.get(i).setProductBigTypeId(productBigType.getProductBigTypeId());
            identificationMapService.save(identificationMaps.get(i));
        }
        return 0;
    }

    //修改商品大类
    @Override
    public int updateBigType(ProductBigTypeUpdateParam productBigTypeUpdateParam) {
        ProductBigType productBigType = new ProductBigType();
        BeanUtils.copyProperties(productBigTypeUpdateParam, productBigType);


        //大类图片修改
        Image image = new Image();
        ProductBigType bigType = getById(productBigTypeUpdateParam.getProductBigTypeId());
        image.setImageId(bigType.getImageId());
        String addr = productBigTypeUpdateParam.getAddr();
        image.setImageAddr(addr);
        imageService.updateById(image);
        productBigType.setImageId(image.getImageId());

        //查询所有大类序号，如有序号等于当前序号则返回1
        QueryWrapper<ProductBigType> wrapper = new QueryWrapper<ProductBigType>();
        wrapper.eq("product_big_type_sort", productBigType.getProductBigTypeSort());
        wrapper.ne("product_big_type_id", productBigTypeUpdateParam.getProductBigTypeId());
        List<ProductBigType> list = list(wrapper);
        if (list.size() > 0) {
            return 1;
        }
        //大类修改
        updateById(productBigType);
        //鉴定图定义修改
        QueryWrapper<IdentificationMap> wrapper1 = new QueryWrapper<IdentificationMap>();
        wrapper1.eq("product_big_type_id", productBigTypeUpdateParam.getProductBigTypeId());
        identificationMapService.remove(wrapper1);
        List<IdentificationMap> identificationMaps = productBigTypeUpdateParam.getIdentificationMaps();
        for (int i = 0; i < identificationMaps.size(); i++) {
            identificationMaps.get(i).setProductBigTypeId(productBigType.getProductBigTypeId());
            identificationMapService.save(identificationMaps.get(i));
        }
        return 0;
    }

    //删除大类
    @Override
    public void deleteById(Integer bigTypeId) {
        System.out.println(bigTypeId);
        //查询是否有商品实体
        ResultDto resultDto = productEntityCient.selectByBigTypeId(bigTypeId);
        if (!resultDto.getData().toString().equals("[]")){
            throw new ProjectProductException(500, "该大类下还有商品，不能删除");
        }
        //删除鉴定图定义
        QueryWrapper<IdentificationMap> wrapper1 = new QueryWrapper<IdentificationMap>();
        wrapper1.eq("product_big_type_id", bigTypeId);
        identificationMapService.remove(wrapper1);
        //删除图片件
        ProductBigType bigType = getById(bigTypeId);
        String imageAddr = imageService.getById(bigType.getImageId()).getImageAddr();
        //删除图片地址
        imageService.removeById(bigType.getImageId());
        //删除小类
        QueryWrapper<ProductSmallType> wrapper2 = new QueryWrapper<ProductSmallType>();
        wrapper2.eq("product_big_type_id", bigTypeId);
        List<ProductSmallType> list = productSmallTypeService.list(wrapper2);
        List<Integer> productSmallTypeIds=new ArrayList<>();
        for (ProductSmallType productSmallType : list) {
            productSmallTypeIds.add(productSmallType.getProductSmallTypeId());
        }
        productSmallTypeService.remove(wrapper2);
        //删除大类品牌中间表
        QueryWrapper<ProductBrandBigType> wrapper3 = new QueryWrapper<ProductBrandBigType>();
        wrapper3.eq("product_big_type_id", bigTypeId);
        productBrandBigTypeService.remove(wrapper3);
        //删除类型
        if(productSmallTypeIds.size()!=0){
        QueryWrapper<Product> wrapper4 = new QueryWrapper<Product>();
        wrapper4.in("product_small_type_id", productSmallTypeIds);
        productService.remove(wrapper4);}
        //删除大类
        removeById(bigTypeId);
    }
}
